<?
include("../common/auth.php");
include('../../jq/com/errBack.php');
include("../class/ProductInfo.php");
$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s   = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));
$pid = abs($_REQUEST['pid']);
$p   = new ProductInfo($s,$pid);
$orderMonth=$_REQUEST['orderMonth'];
$str="select t.id as tid,t.storage,t.storage_open from uu_products p join uu_jq_ticket t on t.pid=p.id left join uu_land_f f on f.pid=p.id where p.id=$pid limit 1";
$GLOBALS['le']->query($str);
$p_info=$GLOBALS['le']->fetch_assoc();
$monthStart=$orderMonth."-01";
$monthEnd=$orderMonth."-".date("t",strtotime($monthStart));
$mintime=$p->getMinValidTime();
$maxtime=$p->getMaxValidTime();
$begintime=$mintime;

if($begintime<$monthStart){
    $begintime=$monthStart;
}
$endtime=$monthEnd;
if($begintime>$endtime){
    $begintime=$endtime;
}
//ʣ���ܿ��
$storageXml=$s->Dynamic_Storage_Check($p_info['tid'],2,NUll,$p_info['storage_open']);
if($p_info['storage']==-1){
    $totalStorage=-1;
}else{
    $storageXml=simplexml_load_string($storageXml);
    $useStorage=$storageXml->Rec->UUXstr;
    $totalStorage=$p_info['storage']-$useStorage;
}
$account = $_SESSION['saccount'];
$memberID = $_SESSION['sid'];
$prices=$p->getPeriodPrice($begintime,$endtime,$account,1,$memberID);
if($_SESSION['account']=='100005') {
//    print_r($prices);
}
$GLOBALS['le']->connectsp();
$jsonData=array();
$dstorage=array();
$tid=$p_info['tid'];
for($i=0,$days=ceil((strtotime($endtime)-strtotime($begintime))/3600/24)+1;$i<$days;$i++){
    $date=date("Y-m-d",strtotime($begintime)+$i*24*3600);
    $lesp=new go_sql();
    $lesp->connectsp();
    $qstr="CALL GSTORAGE('$date',$tid)";
    $lesp->querysp($qstr);
    $row=$lesp->fetch_arraysp();
    $dstorage[$date]=$lesp->f("cc");
    $lesp->close();
}
//TODO::获取差价
$aid = intval($_GET['aid']);
//CHECK AID
$sql = "SELECT apply_did FROM uu_products WHERE id=$pid LIMIT 1";
$GLOBALS['le']->query($sql);
$GLOBALS['le']->fetch_assoc();
$said = $GLOBALS['le']->f('apply_did');
//echo $said;
if ($said==$aid) {
    $aid = 0;
}
$diff_price = $p->dpriceBeta($_SESSION['memberID'], $aid);
//END
foreach($prices as $date=>$price){
    $remain=$totalStorage;
    if($price['storage']!=-1){
        $remain=$price['storage']-$dstorage[$date];
    }
    if($totalStorage!=-1){
        $remain=min((float)$remain,(float)$totalStorage);
    }
    $jsonData[]=array("date"=>$date,
        "sprice"=>($price['sprice']+ $diff_price)/100 ,
        "lprice"=>($price['lprice']+$diff_price)/100 ,
        "uid"=>$price['uid'],
        "storage"=>$price['storage'],
        "sale"=>$dstorage[$date],
        "remain"=>$remain
    );
}
$data["pricelists"]=$jsonData;
$data["about"]["mintime"]=strtotime($mintime);
$data["about"]["maxtime"]=strtotime($maxtime);
$data["about"]["totalStorage"]=$totalStorage;
if($_REQUEST['datatype']=="jsonp"){
    echo $_REQUEST['callback']."(".json_encode($data).")";
}else{
    echo json_encode($data);
}
?>